<?

/*******************************************************************************************************
/*	MY_Model extension for CodeIgniter
/*	Author: Jesse Terry (jessedterry@gmail.com)
/*	This library performs the following:
/*
/*	- Enables a model to easily generate page links
/*	- Gathers the current page via uri_to_assoc
/*	- Prepares a generic $db_array to save based upon post variables that match elements in the validation fields
/*	- Sets the specified fields and rules from a function in the model and returns the validation result
/*	- Can set basic query statements based on $params array
/*
/*	CREATING PAGE LINKS
/*
/*	Directly call the following method after a query that contains a limit and offset:
/*	$this->set_page_links();
/*	Now in a view, the following variable is available to echo the page links:
/*	$this->mdl_name->page_links
/*
/*	PREPARING DATA TO SAVE TO THE DATABASE
/*
/*	A controller can call the following method to quickly generate an array to pass to a database table:
/*	$db_array = $this->mdl_name->prep_db_array();
/*	This method relies on $this->fields as having been set in a validation method in the model
/*
/*	VALIDATING FORM DATA
/*
/*	A model can contain a validation method containing $this->fields and $this->rules
/*	That method can then use $this->_validate to return the validation results
/*
/*	For example:
/*
/*	function validate_register() {
/*
/*		$this->fields = array(
/*		'email_address'	=>	'Email Address',
/*		'password'		=>	'Password');
/*
/*		$this->rules = array(
/*		'email_address'	=>	'required|valid_email',
/*		'password'		=>	'required');
/*
/*		return $this->_validate();
/*
/*	}
/*
/*	Now the controller can simply call the following to determine if the form has validated
/*
/*	if ($this->mdl_name->validate_register()) {
/*
/*		$this->mdl_name->save($this->mdl_name->prep_db_array());
/*
/*	}
/*
/******************************************************************************************************/

class MY_Model extends Model {

	var $fields;

	var $rules;

	var $page_links;

	var $limit;

	var $admin;

	function MY_Model() {

		parent::Model();

	}

	function set_page_links() {

		$all_sql = substr($this->db->last_query(), 0, strpos($this->db->last_query(), 'LIMIT'));

		$all_query = $this->db->query($all_sql);

		$this->load->library('pagination');

		$page_config = array(
		'base_url' 		=>	$this->_remove_page_uri(),
		'total_rows'	=>	$all_query->num_rows(),
		'per_page'		=>	$this->limit,
		'num_links'		=>	10,
		'next_link'		=>	'Next',
		'prev_link'		=>	'Previous',
		'cur_page'		=>	$this->page);

		if (file_exists('./system/application/views/' . $this->inktype->site_template_folder . '/page_link_config.php')) {

			include ('./system/application/views/' . $this->inktype->site_template_folder . '/page_link_config.php');

			if (isset($page_link_config) AND is_array($page_link_config)) {

				foreach ($page_link_config as $key=>$value) {

					$page_config[$key] = $value;

				}

			}

		}

		$this->pagination->initialize($page_config);

		$this->page_links = $this->pagination->create_links();

	}

	function prep_db_array() {

		// takes $_POST elements with matching elements in validation fields and prepares db array

		$db_array = array();

		foreach (array_keys($this->fields) as $field_key) {

			if (key_exists($field_key, $_POST)) {

				$db_array[$field_key] = $this->input->post($field_key);

			}

		}

		return $db_array;

	}

	function prep_validation($object) {

		// takes a database record as object and populates validation elements for form

		if (is_array($object) AND count($object) == 1) {

			$object = $object[0];

		}

		foreach ($object as $key=>$value) {

			$this->validation->{$key} = $object->{$key};

		}

	}

	function prep_params($params) {

		// the model's get() function can use this function if it accepts a $params array
		// this function should only be use to set common query statements
		// the model should set statements which are specific to that table or data object

		if (isset($params['select'])) {

			$this->db->select($params['select']);

		}

		if (isset($params['where'])) {

			if (is_array($params['where'])) {

				foreach ($params['where'] AS $field=>$value) {

					$this->db->where($field, $value);

				}

			}

			else {

				$this->db->where($params['where']);

			}

		}

		if (isset($params['order_by'])) {

			$this->db->order_by($params['order_by']);

		}

		if (isset($params['limit'])) {

			$this->db->limit($params['limit']);

			$this->limit = $params['limit'];

		}
		
		if (isset($params['group_by'])) {
			
			$this->db->group_by($params['group_by']);
			
		}

		if (isset($params['page'])) {

			$this->page = $params['page'];

		}

		if (isset($params['uri_assoc'])) {

			$uri_assoc = $this->uri->uri_to_assoc($params['uri_assoc']);

			if (isset($uri_assoc['page'])) {

				$this->page = $uri_assoc['page'];

			}

		}

	}

	function _remove_page_uri() {

		// if no uri_string() exists, the default site_name and default_view should be used

		$uri_array = explode('/', $this->uri->uri_string());

		unset($uri_array[0]);

		if (count($uri_array) == 1) {

			// this assumes that the only site indicator exists in the url

			// $uri_array[] = 'index';

			$uri_array[] = $this->inktype->site_template_default_view;

		}

		elseif (!count($uri_array)) {

			// this assumes that this is the default page with no site indicator or other uri vars

			$uri_array[] = $this->inktype->site_url_title;

			$uri_array[] = $this->inktype->site_template_default_view;

		}

		$page_key = array_search('page', $uri_array);

		if (is_numeric($page_key)) {

			unset ($uri_array[$page_key]);

			unset ($uri_array[$page_key + 1]);

		}

		$uri_string = site_url(implode('/', $uri_array) . '/page');

		return $uri_string;

	}

	function _validate() {

		$this->validation->set_fields($this->fields);

		$this->validation->set_rules($this->rules);

		if ($this->validation->run()) {

			return TRUE;

		}

		else {

			return FALSE;

		}

	}

}

?>